# Summary: Creates SI D

##############################################
#----------------- SI D ---------------------#
##############################################

options(scipen=999)
rm(list = ls())

# Load libraries
library(tidyverse)
library(lmtest)
library(sandwich)
library(margins)
library(DescTools)
library(insight)

# Set working directory
# setwd('~/Dataverse/')

# Load datasets
load(file = "./Data/df.RData")
load(file = "./Data/df_long.RData")

#############
# TABLE D.5 #
#############

m1 = glm(considered_running ~ racegender +
           political_interest +
           n_offices_qualified +
           partyID +
           encouraged +
           AGE7 +
           education +
           income +
           married, 
         data = df,
         family = binomial)

m1_robust = coeftest(m1, vcov. = vcovHC(m1, type="HC1"))

sink("./SI_D_Results/tableD5.txt")
print("TABLE D.5: Logistic Model Results: Gender and Race")
m1_robust
nrow(m1$model) # N observations
sink()

#############
# TABLE D.6 #
#############

m2 = glm(considered_running ~ racegender + office +
           racegender*office +
           political_interest +
           qualified_office +
           partyID +
           encouraged +
           AGE7 +
           education +
           income +
           married, 
         data = df_long,
         family = binomial)

m2_robust = coeftest(m2, vcov. = vcovCL(m2, vcov = vcovCL, cluster = ~CaseId))

sink("./SI_D_Results/tableD6.txt")
print("TABLE D.6: Logistic Model Results: Gender, Race and Office")
m2_robust
nrow(m2$model) # N observations
sink()


#############
# TABLE D.7 #
#############

m3 = glm(considered_running ~ racegender + encouraged +
           racegender*encouraged +
           political_interest +
           n_offices_qualified +
           partyID +
           AGE7 +
           education +
           income +
           married, 
         data = df,
         family = binomial)

m3_robust = coeftest(m3, vcov. = vcovHC(m3, type="HC1"))

sink("./SI_D_Results/tableD7.txt")
print("TABLE D.7: Logistic Model Results: Gender, Race and Encouragement")
m3_robust
nrow(m3$model) # N observations
sink()
